package csu.webcourse.mypetstore.web.servlet;

import csu.webcourse.mypetstore.domain.Account;
import csu.webcourse.mypetstore.domain.Category;
import csu.webcourse.mypetstore.domain.Product;
import csu.webcourse.mypetstore.service.AccountService;
import csu.webcourse.mypetstore.service.CategoryService;
import csu.webcourse.mypetstore.service.LogService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class EditAccountServlet extends HttpServlet {

    private AccountService accountService;
    private Account account;
    private CategoryService categoryService;
    private List<Product> myList;

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        account = (Account)session.getAttribute("loginAccount");
        accountService = new AccountService();
        categoryService = new CategoryService();
        this.account.setStatus("OK");
        this.account.setUsername(req.getParameter("username"));
        this.account.setPassword(req.getParameter("password"));
        this.account.setFirstName(req.getParameter("account.firstName"));
        this.account.setLastName(req.getParameter("account.lastName"));
        this.account.setEmail(req.getParameter("account.email"));
        this.account.setPhone(req.getParameter("account.phone"));
        this.account.setAddress1(req.getParameter("account.address1"));
        this.account.setAddress2(req.getParameter("account.address2"));
        this.account.setCity(req.getParameter("account.city"));
        this.account.setState(req.getParameter("account.state"));
        this.account.setZip(req.getParameter("account.zip"));
        this.account.setCountry(req.getParameter("account.country"));
        this.account.setLanguagePreference(req.getParameter("account.languagePreference"));
        this.account.setFavouriteCategoryId(req.getParameter("account.favouriteCategoryId"));
        this.account.setListOption(true);
        this.account.setBannerOption(true);
        accountService.updateAccount(account);
        account = accountService.getAccount(account.getUsername());
        myList = categoryService.getProductListByCategory(account.getFavouriteCategoryId());

        Account account = (Account) session.getAttribute("loginAccount");
        if (account != null) {
            HttpServletRequest httpRequest = req;
            String strBackUrl = "http://" + req.getServerName() + ":" + req.getServerPort()
                    + httpRequest.getContextPath() + httpRequest.getServletPath() + "?" + (httpRequest.getQueryString());

            LogService logService = new LogService();
            //最后加入的信息“XXXXX”应当为该界面的信息以及一些商品信息
            String logInfo = logService.logInfo(" ") + strBackUrl + "用户修改信息成功";
            logService.insertLogInfo(account.getUsername(), logInfo);
        }

        resp.sendRedirect("editAccountForm");
    }
}
